<?php

namespace plugin\webman\gateway;

use app\common\MsgType;
use App\model\ChatHistory;
use App\model\ExitLog;
use App\model\Guild;
use App\model\GuildMember;
use app\model\Room;
use app\model\User;
use App\service\CommonService;
use GatewayWorker\Lib\Gateway;
use support\Common;
use support\Constant;
use support\Log;
use Workerman\Timer;

class Events
{

    public static function onWorkerStart($worker)
    {
        Log::info('onWorkerStart');
    }

    public static function onConnect($client_id)
    {
        Log::info(var_export(['onConnect', $client_id], true));
        // Gateway::sendToClient($client_id, json_encode(['ping' => 123]));
    }

    public static function onWebSocketConnect($client_id, $data)
    {
        //        Log::info('onWebSocketConnect');
        //        // 拿到连接时携带的token 查询uid
        //        $getData = $data['get'];
        //        $uuid = $getData['uuid'];//加密UUID
        //        $nickname = $getData['nickname'];//唯一昵称
        //        $userModel = new User();
        ////        $userInfo = $userModel -> getUserInfoByToken($getData['uuid']);
        //
        //        $userinfo = $userModel->where(['uuid' => $uuid])->first();
        //        if (empty($userinfo))
        //        {
        //            $userinfo->uuid = $uuid;
        //            $userinfo->nickname = $nickname;
        //            $userinfo->save();
        //        }
        //
        //        Gateway::bindUid($client_id,$uuid);
        //
        //        send($client_id,'连接成功',[
        //            'client_id' => $client_id,
        //            'type' => MsgType::LOGIN,
        //        ]);
    }

    /**
     * @throws \Exception
     */
    public static function onMessage($client_id, $message)
    {
        $message = json_decode($message, true);
        Log::info(var_export([$message, 'onMessage'], true));

        $type = $message['type'];
        switch ($type) {
            //登录绑定
            case MsgType::C_1000:
                Gateway::bindUid($client_id, $message['uuid']);
                Gateway::sendToUid($message['uuid'], json_encode([
                    'type' => self::getServerType($type),
                ]));
                break;
            //ping
            case MsgType::C_1001:
                print_r(($message['uuid']));
                Gateway::sendToUid($message['uuid'], json_encode([
                    'type' => self::getServerType($type),
                ]));
                break;

            //    收到消息
            case MsgType::C_2001:
                $data = [
                    'type' => $message['msg_type'],
                    'uuid' => $message['uuid'],
                    'nickname' => $message['nickname'],
                    'content' => $message['content'],
                    'ip' => $_SERVER['REMOTE_ADDR'] ?? '',
                ];
                ChatHistory::create($data);
                Gateway::sendToAll(json_encode([
                    'type' => self::getServerType($type),
                    'info' => $data,
                ]));
                break;
            //    请求获得历史聊天消息
            case MsgType::C_2002:
                $list = CommonService::chatLists(50);
                Gateway::sendToUid($message['uuid'], json_encode([
                    'type' => self::getServerType($type),
                    'list' => $list,
                ]));
                break;
            //   发送跑马灯消息
            case MsgType::C_3001:
                CommonService::sendPaomadeng($message['content']);
                break;
            default:
                break;
        }
    }


    public static function getServerType($type)
    {
        return intval($type);
    }

    public static function onClose($client_id)
    {
        Log::info('onClose:' . $client_id);
    }


}
